home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / system-tools-backends-2.0 / scripts / Time / TimeDate.pm < prev   
Encoding:
Perl POD Document  |  2009-04-09  |  9.0 KB  |  387 lines

  1. #-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  
  3. # Time/Date Configuration handling
  4. #
  5. # Copyright (C) 2000-2001 Ximian, Inc.
  6. #
  7. # Authors: Hans Petter Jansson <hpj@ximian.com>
  8. #          Carlos Garnacho     <carlosg@gnome.org>
  9. #          Grzegorz Golawski <grzegol@pld-linux.org> (PLD Support)
  10. #          James Ogley <james@usr-local-bin.org> (SuSE 9.0 support)
  11. #
  12. # This program is free software; you can redistribute it and/or modify
  13. # it under the terms of the GNU Library General Public License as published
  14. # by the Free Software Foundation; either version 2 of the License, or
  15. # (at your option) any later version.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. # GNU Library General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU Library General Public License
  23. # along with this program; if not, write to the Free Software
  24. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  25.  
  26. package Time::TimeDate;
  27.  
  28. use File::Copy;
  29.  
  30. sub get_utc_time
  31. {
  32.   my (%h, $trash);
  33.  
  34.   ($h{"second"}, $h{"minute"}, $h{"hour"}, $h{"monthday"}, $h{"month"}, $h{"year"},
  35.    $trash, $trash, $trash) = gmtime (time);
  36.  
  37.   $h{"year"} += 1900;
  38.  
  39.   return \%h;
  40. }
  41.  
  42. # This function will force date format when setting time
  43. sub change_timedate
  44. {
  45.   my ($time) = @_;
  46.   my ($command);
  47.  
  48.   my $system_table = {
  49.     "Linux"   => "date -u %02d%02d%02d%02d%04d.%02d",
  50.     "FreeBSD" => "date -u -f %%m%%d%%H%%M%%Y.%%S  %02d%02d%02d%02d%04d.%02d",
  51.     "SunOS"   => "date -u %02d%02d%02d%02d%04d.%02d",
  52.   };
  53.  
  54.   $command = sprintf ($$system_table {$Utils::Backend::tool{"system"}},
  55.                       $$time{"month"} + 1, $$time{"monthday"},
  56.                       $$time{"hour"},  $$time{"minute"}, 
  57.                       $$time{"year"},  $$time{"second"});
  58.  
  59.   &Utils::Report::do_report ("time_localtime_set", $command);
  60.   return &Utils::File::run ($command);
  61. }
  62.  
  63. sub set_utc_time
  64. {
  65.   my ($time) = @_;
  66.   my ($res);
  67.  
  68.   $res = &change_timedate ($time);
  69.  
  70.   return -1 if $res;
  71.   return 0;
  72. }
  73.  
  74. sub time_sync_hw_from_sys
  75. {
  76.   &Utils::File::run ("hwclock --systohc") if ($$tool{"system"} eq "Linux");
  77.   return 0;
  78. }
  79.  
  80. sub get_timezone
  81. {
  82.   my ($local_time_file, $zoneinfo_dir) = @_;
  83.   local *TZLIST;
  84.   my $zone;
  85.   my $size_search;
  86.   my $size_test;
  87.  
  88.   # First check whether it's a link, if it is, it's pretty straighforward
  89.   $zone = readlink ($local_time_file);
  90.  
  91.   if ($zone && $zone =~ /^$zoneinfo_dir/)
  92.   {
  93.     $zone =~ s/^$zoneinfo_dir\/+//;
  94.     return $zone;
  95.   }
  96.  
  97.   *TZLIST = &Utils::File::open_read_from_names($zoneinfo_dir . "/zone.tab");
  98.   if (not *TZLIST) { return; }
  99.  
  100.   &Utils::Report::do_report ("time_timezone_scan");
  101.  
  102.   # Get the filesize for /etc/localtime so that we don't have to execute
  103.   # a diff for every file, only for file with the correct size. This speeds
  104.   # up loading 
  105.   $size_search = (stat ($local_time_file))[7];
  106.  
  107.   while (<TZLIST>)
  108.   {
  109.     if (/^\#/) { next; }                   # Skip comments.
  110.     ($d, $d, $zone) = split /[\t ]+/, $_;  # Get 3rd column.
  111.     chomp $zone;                           # Remove linefeeds.
  112.  
  113.  
  114.     # See if this zone file matches the installed one.
  115.     &Utils::Report::do_report ("time_timezone_cmp", $zone);
  116.     $size_test = (stat("$zoneinfo_dir/$zone"))[7];
  117.     if ($size_test eq $size_search)
  118.     {
  119.       if (!&Utils::File::run ("diff $zoneinfo_dir/$zone $local_time_file"))
  120.       {
  121.         # Found a match.
  122.         last;
  123.       }
  124.     }
  125.     
  126.     $zone = "";
  127.   }
  128.   
  129.   close (TZLIST);
  130.   return $zone;
  131. }
  132.  
  133. sub set_timezone
  134. {
  135.   my ($localtime, $zonebase, $timezone) = @_;
  136.  
  137.   &Utils::Report::enter ();
  138.   &Utils::Report::do_report ("time_timezone_set", $timezone);
  139.  
  140.   $tz = "$zonebase/$timezone";
  141.  
  142.   if (stat($tz) ne "")
  143.   {
  144.     unlink $localtime;  # Important, since it might be a symlink.
  145.     
  146.     &Utils::Report::enter ();
  147.     $res = copy ($tz, $localtime);
  148.     &Utils::Report::leave ();
  149.     return -1 unless $res;
  150.     return 0;
  151.   }
  152.  
  153.   &Utils::Report::leave ();
  154.   return -1;
  155. }
  156.  
  157. sub get_dist
  158. {
  159.   my %dist_map =
  160.   (
  161.    "redhat-6.2"      => "redhat-6.2",
  162.    "redhat-7.0"      => "redhat-6.2",
  163.    "redhat-7.1"      => "redhat-6.2",
  164.    "redhat-7.2"      => "redhat-6.2",
  165.    "redhat-7.3"      => "redhat-6.2",
  166.    "redhat-8.0"      => "redhat-6.2",
  167.    "mandrake-9.0"    => "redhat-6.2",
  168.    "debian-3.0"      => "debian-3.0",
  169.    "debian-3.1"      => "debian-3.0",
  170.    "debian-4.0"      => "debian-3.0",
  171.    "debian-5.0"      => "debian-3.0",
  172.    "debian-testing"  => "debian-3.0",
  173.    "ubuntu-7.04"     => "debian-3.0",
  174.    "suse-9.0"        => "redhat-6.2",
  175.    "slackware-9.1.0" => "redhat-6.2",
  176.    "gentoo"          => "redhat-6.2",
  177.    "archlinux"       => "archlinux",
  178.    "pld-1.0"         => "redhat-6.2",
  179.    "vine-3.0"        => "redhat-6.2",
  180.    "freebsd-5"       => "redhat-6.2",
  181.    "solaris-2.11"    => "solaris-2.11",
  182.    );
  183.  
  184.   return $dist_map{$Utils::Backend::tool{"platform"}};
  185. }
  186.  
  187. sub conf_get_parse_table
  188. {
  189.   my %dist_tables =
  190.   (
  191.    "redhat-6.2" =>
  192.    {
  193.      fn =>
  194.      {
  195.        ZONEINFO     => "/usr/share/zoneinfo",
  196.        LOCAL_TIME   => "/etc/localtime"
  197.      },
  198.      table =>
  199.      [
  200.       [ "local_time",   \&get_utc_time ],
  201.       [ "timezone",     \&get_timezone, [LOCAL_TIME, ZONEINFO] ],
  202.      ]
  203.    },
  204.  
  205.    "debian-3.0" =>
  206.    {
  207.      fn =>
  208.      {
  209.        ZONEINFO     => "/usr/share/zoneinfo",
  210.        LOCAL_TIME   => "/etc/localtime"
  211.      },
  212.      table =>
  213.      [
  214.       [ "local_time",   \&get_utc_time ],
  215.       [ "timezone",     \&get_timezone, [LOCAL_TIME, ZONEINFO] ],
  216.      ]
  217.    },
  218.  
  219.    "archlinux" =>
  220.    {
  221.      fn =>
  222.      {
  223.        RC_CONF      => "/etc/rc.conf",
  224.        ZONEINFO     => "/usr/share/zoneinfo",
  225.        LOCAL_TIME   => "/etc/localtime"
  226.      },
  227.      table =>
  228.      [
  229.       [ "local_time",   \&get_utc_time ],
  230.       [ "timezone",     \&Utils::Parse::get_sh, RC_CONF, TIMEZONE ],
  231.      ]
  232.    },
  233.  
  234.    "solaris-2.11" =>
  235.    {
  236.      fn =>
  237.      {
  238.        NTP_CONF  => "/etc/inet/ntp.conf",
  239.        INIT_FILE => "/etc/default/init"
  240.      },
  241.      table =>
  242.      [
  243.       [ "local_time", \&get_utc_time ],
  244.       [ "timezone",   \&Utils::Parse::get_sh, INIT_FILE, TZ ],
  245.      ]
  246.    },
  247.   );
  248.  
  249.   my $dist = &get_dist();
  250.   return %{$dist_tables{$dist}} if $dist;
  251.  
  252.   &Utils::Report::do_report ("platform_no_table", $Utils::backend::tool{"platform"});
  253.   return undef;
  254. }
  255.  
  256. sub conf_get_replace_table
  257. {
  258.   my %dist_tables =
  259.   (
  260.    "redhat-6.2" =>
  261.    {
  262.      fn =>
  263.      {
  264.        ZONEINFO     => "/usr/share/zoneinfo",
  265.        LOCAL_TIME    => "/etc/localtime"
  266.      },
  267.      table =>
  268.      [
  269.       [ "timezone",    \&set_timezone, [LOCAL_TIME, ZONEINFO] ],
  270.       [ "local_time",  \&set_utc_time ],
  271.      ]
  272.    },
  273.        
  274.    "debian-3.0" =>
  275.    {
  276.      fn =>
  277.      {
  278.        ZONEINFO     => "/usr/share/zoneinfo",
  279.        LOCAL_TIME   => "/etc/localtime",
  280.        TIMEZONE     => "/etc/timezone"
  281.      },
  282.      table =>
  283.      [
  284.       [ "timezone",    \&set_timezone, [LOCAL_TIME, ZONEINFO] ],
  285.       [ "timezone",    \&Utils::Replace::set_first_line, TIMEZONE ],
  286.       [ "local_time",  \&set_utc_time ],
  287.      ]
  288.    },
  289.  
  290.    "archlinux" =>
  291.    {
  292.      fn =>
  293.      {
  294.        RC_LOCAL     => "/etc/rc.local",
  295.        ZONEINFO     => "/usr/share/zoneinfo",
  296.        LOCAL_TIME   => "/etc/localtime",
  297.      },
  298.      table =>
  299.      [
  300.       [ "timezone",    \&Utils::Replace::set_sh, RC_LOCAL, TIMEZONE ],
  301.       [ "timezone",    \&set_timezone, [LOCAL_TIME, ZONEINFO] ],
  302.       [ "local_time",  \&set_utc_time ],
  303.      ]
  304.    },
  305.  
  306.    "solaris-2.11" =>
  307.    {
  308.      fn =>
  309.      {
  310.        ZONEINFO   => "/usr/share/lib/zoneinfo",
  311.        LOCAL_TIME => "/etc/localtime",
  312.        INIT_FILE  => "/etc/default/init"
  313.      },
  314.      table =>
  315.      [
  316.       [ "timezone",    \&Utils::Replace::set_sh, INIT_FILE, TZ ],
  317.       [ "timezone",    \&set_timezone, [LOCAL_TIME, ZONEINFO] ],
  318.       [ "local_time",  \&set_utc_time ],
  319.      ]
  320.    },
  321.   );
  322.  
  323.   my $dist = &get_dist ();
  324.   return %{$dist_tables{$dist}} if $dist;
  325.  
  326.   &Utils::Report::do_report ("platform_no_table", $Utils::Backend::tool{"platform"});
  327.   return undef;
  328. }
  329.  
  330. sub get
  331. {
  332.   my %dist_attrib;
  333.   my $hash;
  334.  
  335.   %dist_attrib = &conf_get_parse_table ();
  336.  
  337.   $hash = &Utils::Parse::get_from_table ($dist_attrib{"fn"},
  338.                                          $dist_attrib{"table"});
  339.   $h = $$hash {"local_time"};
  340.  
  341.   return ($$h {"year"}, $$h {"month"},  $$h {"monthday"},
  342.           $$h {"hour"}, $$h {"minute"}, $$h {"second"},
  343.           $$hash{"timezone"});
  344. }
  345.  
  346. sub set
  347. {
  348.   my (@config) = @_;
  349.   my ($hash, %localtime);
  350.  
  351.   %localtime = (
  352.     "year"     => $config[0],
  353.     "month"    => $config[1],
  354.     "monthday" => $config[2],
  355.     "hour"     => $config[3],
  356.     "minute"   => $config[4],
  357.     "second"   => $config[5]
  358.   );
  359.  
  360.   $$hash{"local_time"} = \%localtime;
  361.   $$hash{"timezone"}   = $config[6];
  362.  
  363.   %dist_attrib = &conf_get_replace_table ();
  364.  
  365.   $res = &Utils::Replace::set_from_table ($dist_attrib{"fn"}, $dist_attrib{"table"}, $hash);
  366.   &time_sync_hw_from_sys ();
  367.  
  368.   return $res;
  369. }
  370.  
  371. sub get_files
  372. {
  373.   my (%dist_attrib, $fn, $f, $files);
  374.  
  375.   %dist_attrib = &conf_get_replace_table ();
  376.   $fn = $dist_attrib {"fn"};
  377.  
  378.   foreach $f (values %$fn)
  379.   {
  380.     push @$files, $f;
  381.   }
  382.  
  383.   return $files;
  384. }
  385.  
  386. 1;
  387.